<?php
class ChannelStoreModel
{
    /*
        添加第三方店铺信息
        params
            channel_id 第三方渠道id
            store_id 快方药店id
            shop_id 第三方店铺id
            shop_name 第三方店铺名称
            shop_desc 第三方店铺描述
            start_time 营业开始时间
            end_time 营业结束时间
            store_status 店铺状态
            shipping_fee 配送费
    */
    public function addChannelStoreInfo(
        $channel_id = 0, $store_id = 0, $shop_id = 0, 
        $shop_name = "", $shop_desc = "", 
        $start_time = 0, $end_time = 0,
        $store_status = 0, $shipping_fee = 0
    ){
        $Dao = M("kfsy_channel_store.kf_channel_store");

        $data = array();
        $data['fk_channel_id'] = $channel_id;
        $data['fk_store_id'] = $store_id;
        $data['other_shop_id'] = $shop_id;
        $data['other_shop_name'] = $shop_name;
        $data['other_shop_desc'] = $shop_desc;
        $data['shipping_start_time'] = $start_time;
        $data['shipping_end_time'] = $end_time;
        $data['shipping_fee'] = $shipping_fee;
        $data['store_status'] = $store_status;
        $data['createtime'] = date("Y-m-d H:i:s");

        $add = $Dao->add($data);

        if($add == false){
            DLOG("添加第三方店铺失败 sql : " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $add;
    }

    /*
        更新第三方店铺信息
        params
            channel_id 第三方渠道id
            store_id 店铺id
            store_status 店铺状态 0-停业 1-营业 2-下线
    */
    public function updateChannelStoreInfo($channel_id = 0, $store_id = 0, $data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_store");

        $where = sprintf("fk_channel_id = %s AND fk_store_id = %s", $channel_id, $store_id);

        $save = $Dao
            ->where($where)
            ->save($data);

        if($save === false){
            DLOG("更新第三方店铺信息失败 sql: " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $save;
    }

    /*
        更新第三方店铺状态
        params
            channel_id 第三方渠道id
            store_id 店铺id
            store_status 店铺状态 0-停业 1-营业 2-下线
    */
    public function updateChannelStoreData($channel_id = 0, $store_id = 0, $data = array())
    {
        $Dao = M("kfsy_channel_store.kf_channel_store");

        $where = sprintf("fk_channel_id = %s AND fk_store_id = %s", $channel_id, $store_id);

        $save = $Dao
            ->where($where)
            ->save($data);

        if($save === false){
            DLOG("更新第三方店铺信息失败 sql: " . $Dao->getLastSql(), "error", "channel_db");
            return false;
        }

        return $save;
    }

    /*
        获取第三方药店信息
        params
            channel_id 第三方渠道id
            store_id 店铺id
            field 查询字段
    */
    public function getChannelStoreInfo($channel_id = 0, $store_id = 0, $field = '*')
    {
        $Dao = M("kfsy_channel_store.kf_channel_store");

        $where = sprintf("fk_channel_id = %s AND fk_store_id = %s", $channel_id, $store_id);

        $storeInfo = $Dao
            ->field($field)
            ->where($where)
            ->find();

        return $storeInfo;
    }

    /*
        通过渠道ID查本地门店ID和第三方门店ID
        params
            channel_id 第三方渠道id
            field 查询字段
    */
    public function getOtherShopIdByChnId($channel_id = 0)
    {
        $Dao = M("kfsy_channel_store.kf_channel_store");

        $where = sprintf("fk_channel_id = %s", $channel_id);

        $storeInfo = $Dao
            ->field('fk_store_id, other_shop_id')
            ->where($where)
            ->select();

        return $storeInfo;
    }
}
?>